package com.tql.view.controller;

import com.github.pagehelper.PageInfo;
import com.tql.view.pojo.Logs;
import com.tql.view.pojo.Scenery;
import com.tql.view.service.LogsService;
import com.tql.view.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/logs")
public class LogsController {
    @Autowired
    private LogsService logsService;
    @RequestMapping("/toLogs")
    public String toLogs(){
        return "admin/logss";
    }
    @RequestMapping("/selectAllLogs")
    public String selectAllLogs(Model model, @RequestParam(defaultValue = "1")Integer pageNum,
                                @RequestParam(defaultValue = "15") Integer size){
        List<Logs> list=logsService.selectAllLogs(pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/logsDetails";
    }
    @RequestMapping("/selectAllLogsBy")
    public String selectAllLogsBy(@RequestParam("account")String account,
                                      @RequestParam("begintime")String begintime,
                                      @RequestParam("endtime")String endtime,
                                      Model model,
                                  @RequestParam(defaultValue = "1")Integer pageNum,
                                  @RequestParam(defaultValue = "15") Integer size) throws ParseException {
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Date begindate=simpleDateFormat.parse(begintime);
        Date endate=simpleDateFormat.parse(endtime);
        if (begindate!=null){
            begindate= DateUtil.getBeginDate(begindate);
        }
        if (endate!=null){
            endate=DateUtil.getEndDate(endate);
        }
        List<Logs> list=logsService.selectAllLogsBy(pageNum,size,account,begindate,endate);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("begintime",begintime);
        model.addAttribute("endtime",endtime);
        model.addAttribute("account",account);
        return "admin/logsss";
      /*  return logsService.selectAllLogsBy(pageNum,size,account,begindate,endate);*/
       /* return begindate;*/
    }
}
